package com.google.code.bigyang.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class NioSocketAcceptor extends NioAcceptor implements Runnable {

	private ServerSocketChannel serverChannel;
	private int port;
	
	private NioSocketPoller poller = new NioSocketPoller();
	
	public void listen() {
		new Thread(this,"acceptor").start();
		new Thread(poller,"poler").start();
	}
	
	public void config() throws IOException {
		serverChannel = ServerSocketChannel.open();
		serverChannel.configureBlocking(true);
		serverChannel.socket().bind(new InetSocketAddress(port));
		poller.config();
	}
	
	
	public void run() {
		while(true) {
			try {
				SocketChannel channel = serverChannel.accept();
				poller.addToQueue(channel.socket());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void setPort(int port) {
		this.port = port;
	}
	
	
	
}
